﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;

using FRL.DebtChased.Types;

namespace FRL.DebtChased.Repository.PartModels
{

	/// <summary>
	/// 
	/// </summary>
	public class CommissionChartModel : BasePartModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Manager
		{

			/////////////////////////////////////////////////////////////
			// Nested classes

			public class Collector
			{

				/////////////////////////////////////////////////////////////
				// Attrtibutes

				private int m_iUserId;
				public int UserId 
				{
					get { return m_iUserId; }
				}

				public string UserDescription { get; set; }

				public Money Target { get; set; }

				public Money Invoiced { get; set; }
				public Money ToBeInvoiced { get; set; }
				public Money ClientFunds { get; set; }
				public Money AgentFees { get; set; }
				public Money SolicitorFees { get; set; }
				public Money ConfirmedToday { get; set; }

				public Money ToGo
				{
					get { return _calcTotalTogo(); }
				}

				public Money SubTotal
				{
					get { return _calcTotalSubTotal(); }
				}

				/////////////////////////////////////////////////////////////
				// Construction

				public Collector(int iUserId)
				{

					// Initialize member variables
					m_iUserId = iUserId;

				}

				/////////////////////////////////////////////////////////////
				// Operations

				private Money _calcTotalTogo()
				{

					// Calculate the amount remaining
					Money Ftmp = Target - _calcTotalSubTotal();

					// Return variable
					return Ftmp;

				}

				private Money _calcTotalSubTotal()
				{

					// Calculate sub-total
					Money Ftmp = 
						this.Target + 
						this.Invoiced +
						this.ToBeInvoiced + 
						this.ClientFunds +
						this.AgentFees + 
						this.SolicitorFees;

					// Return variable
					return Ftmp;

				}

			}

			/////////////////////////////////////////////////////////////
			// Attributes

			private int m_iUserId;
			public int UserId
			{
				get { return m_iUserId; }
			}

			public string UserDescription { get; set; }

			public Money Target
			{
				get { return _calcTarget(); }
			}

			public Money Invoiced
			{ 
				get { return _calcInvoiced(); } 
			}

			public Money ToBeInvoiced
			{
				get { return _calcToBeInvoiced(); }
			}

			public Money ClientFunds
			{ 
				get { return _calcClientFunds(); } 
			}

			public Money AgentFees
			{ 
				get { return _calcAgentFees(); } 
			}

			public Money SolicitorFees
			{
				get { return _calcSolicitorFees(); } 
			}

			public Money ConfirmedToday
			{
				get { return _calcConfirmedToday(); }
			}

			public Money ToGo
			{
				get { return _calcToGo(); }
			}

			public Money SubTotal
			{
				get { return _calcSubTotal(); }
			}

			/////////////////////////////////////////////////////////////
			// Attributes

			private List<Collector> m_collectorList = new List<Collector>();
			public List<Collector> Collectors
			{
				get { return m_collectorList; }
			}

			/////////////////////////////////////////////////////////////
			// Construction

			public Manager(int iUserId)
			{

				// Initialize member variables
				m_iUserId = iUserId;

			}

			/////////////////////////////////////////////////////////////
			// Operations

			private Money _calcTarget()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if (i1 == 0)
						Ftmp = new Money(m_collectorList[i1].Target);
					else
						Ftmp += m_collectorList[i1].Target;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcInvoiced()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if (i1 == 0)
						Ftmp = new Money(m_collectorList[i1].Invoiced);
					else
						Ftmp += m_collectorList[i1].Invoiced;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcToBeInvoiced()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].ToBeInvoiced);
					else
						Ftmp += m_collectorList[i1].ToBeInvoiced;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcClientFunds()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].ClientFunds);
					else
						Ftmp += m_collectorList[i1].ClientFunds;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcSolicitorFees()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].SolicitorFees);
					else
						Ftmp += m_collectorList[i1].SolicitorFees;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcAgentFees()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].AgentFees);
					else
						Ftmp += m_collectorList[i1].AgentFees;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcConfirmedToday()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].ConfirmedToday);
					else
						Ftmp += m_collectorList[i1].ConfirmedToday;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcToGo()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if (i1 == 0)
						Ftmp = new Money(m_collectorList[i1].ToGo);
					else
						Ftmp += m_collectorList[i1].ToGo;
				}

				// Return variable
				return Ftmp;

			}

			private Money _calcSubTotal()
			{

				// Declare return variable
				Money Ftmp = null;

				// Loop through collectors
				for (int i1 = 0; i1 < m_collectorList.Count; i1++)
				{
					if(i1==0)
						Ftmp = new Money(m_collectorList[i1].SubTotal);
					else
						Ftmp += m_collectorList[i1].SubTotal;
				}

				// Return variable
				return Ftmp;

			}

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Manager> m_managerList = new List<Manager>();
		public List<Manager> Managers
		{
			get { return m_managerList; }
		}

		public Money SubTotal
		{
			get { return _calcSubTotal(); }
		}

		public Money ToGo
		{
			get { return _calcToGo(); }
		}

		public Money ConfirmedToday
		{
			get { return _calcConfirmedToday(); }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public CommissionChartModel(DebtChasedEntities ent)
		{

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private Money _calcSubTotal()
		{

			// Declare return variable
			Money Ftmp = null;

			// Loop through collectors
			for (int i1 = 0; i1 < m_managerList.Count; i1++)
			{
				if(i1==0)
					Ftmp = new Money(m_managerList[i1].SubTotal);
				else
					Ftmp += m_managerList[i1].SubTotal;
			}

			// Return variable
			return Ftmp;

		}

		private Money _calcToGo()
		{

			// Declare return variable
			Money Ftmp = null;

			// Loop through collectors
			for (int i1 = 0; i1 < m_managerList.Count; i1++)
			{
				if(i1==0)
					Ftmp = new Money(m_managerList[i1].ToGo);
				else
					Ftmp += m_managerList[i1].ToGo;
			}

			// Return variable
			return Ftmp;

		}

		private Money _calcConfirmedToday()
		{

			// Declare return variable
			Money Ftmp = null;

			// Loop through collectors
			for (int i1 = 0; i1 < m_managerList.Count; i1++)
			{
				if(i1==0)
					Ftmp = new Money(m_managerList[i1].ConfirmedToday);
				else
					Ftmp += m_managerList[i1].ConfirmedToday;
			}

			// Return variable
			return Ftmp;

		}

	}

}